<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/8
 * Time: 上午11:40
 */

namespace app\index\controller;


use think\Request;
use think\Session;

class Login extends Basecontroller
{
    protected $noNeedLogin = ['index', 'reg', 'logout'];


    /**
     * 登录页
     */
    public function index()
    {

        if ($this->request->isPost()) {
            $account = $this->request->param('phone');
            $password = $this->request->param('password');

            if ($this->user->login($account, $password)) {
                $this->success("登录成功", url('index/index'));

            } else {
                $this->error($this->user->getError());
            }
        }
        return $this->fetch();
    }


    /**
     * 注册
     */
    public function reg()
    {
        if ($this->request->isPost()) {
            $username = $this->request->post('phone');
            $password = $this->request->post('password');
            if ($this->user->register($username, $password)) {
                $this->success("注册成功", url('login/index'));

            } else {
                $this->error($this->user->getError());
            }
        }
        return $this->fetch();

    }

    /**
     * 注销登录
     */
    function logout()
    {
        //注销本站
        $this->user->logout();

        return $this->redirect('login/index');
    }
}